package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.common.assist.json.JsonOperation;
import com.magicode.cms.dao.IHelpDao;
import com.magicode.cms.entiy.base.Help;
import com.magicode.cms.service.IHelpService;
@Service("serviceHelp")
public class HelpServiceImpl extends BaseServiceImpl<Help,IHelpDao> implements IHelpService,UtilPath {

	public List<Map<String,Object>> select(Map<String,String> querywhere){
		try {
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("subTitle", ""+entity_alias_name+".subTitle as subTitle ");
			gridsel.put("code", ""+entity_alias_name+".code as code ");
			gridsel.put("orderCode", ""+entity_alias_name+".orderCode as orderCode ");
			return this.queryMapDataList(hql, gridsel,0 , 0);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}
		@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "标题");
		map.put("width", 200);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "subTitle");
		map.put(json_title, "副标题");
		map.put("width", 200);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "code");
		map.put(json_title, "编号");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "orderCode");
		map.put(json_title, "排序码");
		map.put("width", 100);
		beaninfo.add(map);
		return beaninfo;
	}

	@Override
	public String getHql(Map<String, String> querywhere) throws Exception {
		String hql = " from "+Help.class.getName()+" t where 1 = 1";
		if(null!=querywhere && !querywhere.isEmpty()){
			if(querywhere.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id='"+querywhere.get(field_id)+"'";
			}
			if(querywhere.containsKey(field_title)){
				hql = hql+" and "+entity_alias_name+".title like '%"+querywhere.get(field_title)+"%'";
			}
			if(querywhere.containsKey("subTitle")){
				hql = hql+" and "+entity_alias_name+".subTitle like '%"+querywhere.get("subTitle")+"%'";
			}
			if(querywhere.containsKey("code")){
				hql = hql+" and "+entity_alias_name+".code like '&"+querywhere.get("code")+"%'";
			}
		}
		hql += " order by "+entity_alias_name+".orderCode asc ";
		return hql;
	}

	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {

		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("subTitle", ""+entity_alias_name+".subTitle as subTitle ");
			gridsel.put("code", ""+entity_alias_name+".code as code ");
			gridsel.put("orderCode", ""+entity_alias_name+".orderCode as orderCode ");
			return this.queryMapDataList(hql, gridsel,start , size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
		
	
	}

	@Override
	public List<JsonOperation> getOpts(String url,String id) {
		List<JsonOperation> opts = new ArrayList<JsonOperation> ();
		JsonOperation opt= new JsonOperation();
		opt.setName("修改");
		opt.setUrl(getCtx().getContextPath()+"/"+url+"/getinfo/"+id+"/edit"+url);
		opt.setType("edit");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("删除");
		opt.setUrl(getCtx().getContextPath()+"/"+url+"/"+id+"?_requestMethod=delete");
		opt.setType("delete2");
		opts.add(opt);
		return opts; 
	}
	@Override
	public IHelpDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return daoHelp;
	}
	@Autowired
	@Qualifier("daoHelp")
	IHelpDao daoHelp;
	public IHelpDao getDaoHelp() {
		return daoHelp;
	}
	public void setDaoHelp(IHelpDao daoHelp) {
		this.daoHelp = daoHelp;
	}
	/**
	 * 根据code得到Help
	 */
	@Override
	public List getHelpByCodeList(List<String> codeList) {
		// TODO Auto-generated method stub
		String sql = "select t from "+Help.class.getName()+" t where 1 = 1  ";
		List list = null;
		if(codeList != null && codeList.size() > 0){
			sql += " and t.code in ( ";
			for(String code : codeList){
				sql += "'"+code+"',";
			}
			sql = sql.substring(0, sql.length() - 1);
			sql += ")";
		}
		sql += " order by t.orderCode asc ";
		list = this.queryDataList(sql);
		
		return list;
	}
	
}
